﻿import { Component, OnInit } from '@angular/core';
import { Http, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Movies } from './../../model/movies'

@Component({
    selector: 'movies',
    templateUrl: './app/components/movies/movies.component.html'
})


export class MoviesComponent implements OnInit {
    Movies: Movies[];
    loadingMovies: boolean = true;
    page: string;
    pagesCount: number;
    totalCount: number;
    filterMovies: string;
    constructor(public http: Http) {
    }

    getMovies(): Promise<Movies[]> {
        return this.http.get('/api/movies/')
            .toPromise()
            .then(response => response.json().data as Movies[])
            .catch((error) => { console.log(error) });
    }
    ngOnInit() {
        //初始化时获取第一页数据
        this.search('0');
    }
    search(page: any) {
        page = page || 0;
        let urlSearchParams = new URLSearchParams();
        urlSearchParams.append('page', page.value);
        urlSearchParams.append('pageSize', '6');
        urlSearchParams.append('filter', this.filterMovies);
        console.log('filter'+this.filterMovies);
        this.http.get('/api/movies/', { search: urlSearchParams })
            .toPromise()
            .then((res) => {
                let data = res.json();
                this.Movies = data.Items;
                this.page = data.Page;
                this.pagesCount = data.TotalPages;
                this.totalCount = data.TotalCount;
                this.loadingMovies = false;
                console.log('pagecount' + this.pagesCount);
                if (this.filterMovies && this.filterMovies.length) {

                }
            })
            .catch((error) => {
                console.log(error);
            });
    }
    clearSearch() {
        this.filterMovies = "";
        this.search('0');
    }
}


